package com.zdj.data.net;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.zdj.app.AppConfig;
import com.zdj.data.BuildConfig;
import com.zdj.utils.MyLogUtil;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zdj on 16-5-30.
 */

public class RetrofitClient {

    private static final String TAG = "RetrofitClient";

    private static final String BASE_URL = "http://www.android10.org/myapi/";

    public static final String BASE_URL_MUSIC = BuildConfig.DEBUG ? "http://test.sucai.xingzhijishu.com" : "http://sucai.xingzhijishu.com";


    public static final String BASE_URL_LOCAL = "http://192.168.0.106:8080/rs/";
//    public static final String BASE_URL_LOCAL = "http://192.168.199.130:8080/rs/";

    public static final String XZ_BASE_URL = "http://test.sucai.xingzhijishu.com/api2/";

    private static volatile Retrofit instance;

    public static Retrofit getInstance() {
        if (instance == null) {
            instance = new Retrofit.Builder().baseUrl(BASE_URL_LOCAL)
                    .client(OkHttpNetCenter.getInstance().getmOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
        }
        return instance;
    }

    private static OkHttpClient httpClient;

    private static void initHttpClient() {
        if (httpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.connectTimeout(10, TimeUnit.SECONDS);
            builder.writeTimeout(10, TimeUnit.SECONDS);
            builder.readTimeout(30, TimeUnit.SECONDS);
            builder.addInterceptor(chain -> {
                Request original = chain.request();
                MyLogUtil.e(TAG, "intercept: " + original.toString());
                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .addHeader(AppConfig.AUTHORIZATION, AppConfig.BASIC);

                Request request = requestBuilder.build();
                return chain.proceed(request);
            });
            httpClient = builder.build();
        }
    }

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(XZ_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(initGson())).
                    addCallAdapterFactory(RxJavaCallAdapterFactory.create());

    private static Gson initGson() {
        return new GsonBuilder()
                .registerTypeAdapter(int.class,
                        (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
                            MyLogUtil.e(TAG, "deserialize: " + json.toString());
                            try {
                                return json.getAsInt();
                            } catch (NumberFormatException e) {
                                return -1;
                            }
                        }).create();
    }


    public static <S> S createService(Class<S> serviceClass) {
        initHttpClient();
        Retrofit retrofit = builder.client(httpClient).build();
        return retrofit.create(serviceClass);
    }
}
